#include "apue.h"
#include <unistd.h>
#include <fcntl.h>

#define BUFSIZE 4096

void sync_cat_test(void);

int main(int argc, char const *argv[])
{
    sync_cat_test();
    return 0;
}

void sync_cat_test(void)
{
    ssize_t n;
    int flags;
    char buf[BUFSIZE] = {0};

    /*get file status flag*/
    if ((flags = fcntl(STDOUT_FILENO, F_GETFD, 0)) < 0)
    {
        err_sys("Get file status flag error\n");
    }
    
    /* flags |= O_SYNC; */
    /* set file status flag */
    if ((flags = fcntl(STDOUT_FILENO, F_SETFD, flags)) < 0)
    {
        err_sys("Set file status flag error\n");
    }
    
    /*stdin write to stdout */
    while ((n = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
    {
        if (write(STDOUT_FILENO, buf, n) != n)
        {
            err_sys("Write error\n");
        }
    }
}